<template>
  <div class="app-container">
    <div class="filter-container">
      <statisSearchBar :start-date="searchForm.startDate" :end-date="searchForm.endDate" @transferFilter="handleFilter" />
    </div>
    <el-table v-loading="listLoading" :data="list" :span-method="objectSpanMethod" class="mt-10 hour-table" border height="760">
      <el-table-column label="日期" prop="date" width="110px" align="center"></el-table-column>
      <el-table-column label="时间" prop="t" width="98px" align="center"></el-table-column>
      <el-table-column v-for="(hour, index) in headList" :key="index" align="center" width="60px" :label="hour+'时'">
        <template slot-scope="scope">
          <div class="pre-formatted">{{ scope.row[hour] }}</div>
        </template>
      </el-table-column>

    </el-table>
  </div>
</template>

<script>
import StatisSearchBar from '@/components/StatisSearchBar'
import { getHour } from '@/api/statis'

export default {
  name: 'Hour',
  components: { StatisSearchBar },
  data() {
    return {
      list: [],
      headList: [],
      total: 0,
      listLoading: false,
      searchForm: {
        startDate: new Date(Date.now() - 86400000 * 10),
        endDate: new Date()
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleFilter(query) {
      this.searchForm = query
      this.getList()
    },
    async getList() {
      this.listLoading = true
      const res = await getHour(this.searchForm)
      this.listLoading = false
      this.list = res.data.items
      this.headList = res.data.hours
    },
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 0) {
        if (rowIndex % 6 === 0) {
          return {
            rowspan: 6,
            colspan: 1
          }
        } else {
          return {
            rowspan: 0,
            colspan: 0
          }
        }
      }
    }
  }
}
</script>
<style lang="scss">
.hour-table {
  width: 100%;
  border-collapse: collapse;
}

.hour-table  tr:nth-child(12n+1),
.hour-table  tr:nth-child(12n+2),
.hour-table  tr:nth-child(12n+3),
.hour-table  tr:nth-child(12n+4),
.hour-table  tr:nth-child(12n+5),
.hour-table  tr:nth-child(12n+6) {
  background-color: #fff; /* 第1-6行的颜色 */
}

.hour-table  tr:nth-child(12n+7),
.hour-table  tr:nth-child(12n+8),
.hour-table  tr:nth-child(12n+9),
.hour-table  tr:nth-child(12n+10),
.hour-table  tr:nth-child(12n+11),
.hour-table  tr:nth-child(12n+12) {
  background-color: #f5f5f5; /* 第7-12行的颜色 */
}
</style>
